/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.validator.impl;

import com.genia.toolbox.constants.client.i18n.ErrorDescriptions;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.support.GwtObjectUtils;

/**
 * implementation of
 * {@link com.genia.toolbox.web.gwt.form.client.validator.Validator} that checks
 * if a value is a decimal and if it is in the correct range.
 */
public class DoubleValidator
    extends AbstractSimpleValueSynchronousValidator
{

  /**
   * the minimal acceptable value.
   */
  private double minValue;

  /**
   * the maximal acceptable value.
   */
  private double maxValue;

  /**
   * indicate if empty value is accepted.
   */
  private boolean emptyValueAccepted = false;



  /**
   * constructor.
   */
  public DoubleValidator()
  {
    this(Double.MIN_VALUE, Double.MAX_VALUE);
  }



  /**
   * constructor.
   * 
   * @param minValue
   *          the minimal acceptable value
   * @param maxValue
   *          the maximal acceptable value
   */
  public DoubleValidator(final double minValue, final double maxValue)
  {
    this.minValue = minValue;
    this.maxValue = maxValue;
  }



  /**
   * validate if the item's value is acceptable.
   * 
   * @param value
   *          the value to validate.
   * @return the error message if the value is not correct, <code>null</code>
   *         otherwise.
   */
  @Override
  public GwtI18nMessage validate(final String value)
  {
    if (emptyValueAccepted) {
      if (value == null || value.trim().length() == 0) {
        return null;
      }
    }
    try {
      final double doubleValue = Double.parseDouble(value);
      if (doubleValue < minValue || doubleValue > maxValue) {
        return new GwtI18nMessage(ErrorDescriptions.RANGE_ERROR_KEY, new String[] { String.valueOf(minValue), String.valueOf(maxValue) });
      }
    }
    catch (final Exception e) {
      return new GwtI18nMessage(ErrorDescriptions.DOUBLE_PARSE_ERROR_KEY);
    }
    return null;
  }



  /**
   * getter for the emptyValueAccepted property.
   * 
   * @return the emptyValueAccepted
   */
  public boolean isEmptyValueAccepted()
  {
    return emptyValueAccepted;
  }



  /**
   * setter for the emptyValueAccepted property.
   * 
   * @param emptyValueAccepted
   *          the emptyValueAccepted to set
   */
  public void setEmptyValueAccepted(boolean emptyValueAccepted)
  {
    this.emptyValueAccepted = emptyValueAccepted;
  }



  /**
   * getter for the maxValue property.
   * 
   * @return the maxValue
   */
  public double getMaxValue()
  {
    return maxValue;
  }



  /**
   * setter for the maxValue property.
   * 
   * @param maxValue
   *          the maxValue to set
   */
  public void setMaxValue(double maxValue)
  {
    this.maxValue = maxValue;
  }



  /**
   * getter for the minValue property.
   * 
   * @return the minValue
   */
  public double getMinValue()
  {
    return minValue;
  }



  /**
   * setter for the minValue property.
   * 
   * @param minValue
   *          the minValue to set
   */
  public void setMinValue(double minValue)
  {
    this.minValue = minValue;
  }



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (obj == this) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    // if (getClass() == obj.getClass()) { Waiting for gwt with java 1.5
    if (obj instanceof DoubleValidator) {
      final DoubleValidator oItem = (DoubleValidator) obj;

      if (isEmptyValueAccepted() != oItem.isEmptyValueAccepted()) {
        return false;
      }

      if (getMaxValue() != oItem.getMaxValue()) {
        return false;
      }

      return (getMinValue() == oItem.getMinValue());

    }

    return false;
  }



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by
   * <code>java.util.Hashtable</code>.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return GwtObjectUtils.nullSafeHashCode(new Object[] {
    // getClass(), Waiting for gwt with java 1.5
        new Boolean(isEmptyValueAccepted()), new Double(getMinValue()), new Double(getMaxValue()) });
  }

}
